package com.cxg.fairy.ec.main.personal.address;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.util.toasty.Toasty;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.style.citylist.Toast.ToastUtils;
import com.lljjcoder.style.citypickerview.CityPickerView;
import com.rengwuxian.materialedittext.MaterialEditText;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by CFairy
 */

public class NewAddressDelegate extends LatteDelegate implements ISuccess {

    @BindView(R2.id.met_receive_username)
    MaterialEditText met_username;

    @BindView(R2.id.met_receive_address)
    MaterialEditText met_address;

    @BindView(R2.id.met_receive_phoneno)
    MaterialEditText met_phone;

    @BindView(R2.id.met_receive_detail_address)
    MaterialEditText met_detail_address;

    @OnClick(R2.id.met_receive_address)
    void onClickPopCityPicker() {
        hideAllSoftInput();
        popCityPicker();
    }

    private AddressDelegate addressDelegate;

    public NewAddressDelegate(AddressDelegate delegate) {
        this.addressDelegate = delegate;
    }

    CityPickerView mPicker=new CityPickerView();
    CityConfig cityConfig = new CityConfig.Builder().build();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPicker.init(this.getActivity());
    }

    @OnClick(R2.id.new_receive_address_btn)
    void doAddNewAddress() {
        int usernameLen = String.valueOf(met_username.getText()).length();
        int addressLen = String.valueOf(met_address.getText()).length();
        String phone = String.valueOf(met_phone.getText());
        if (usernameLen >1 && usernameLen < 10 && addressLen > 2 &&phone.length()>10 && phone.length() < 15 && phone.matches("[0-9]+")){
            RestClient.builder()
                    .url("createAddress")
                    .loader(getContext())
                    .params("acceptName", met_username.getText().toString())
                    .params("uuid", AccountManager.getSignUUID())
                    .params("area", met_address.getText().toString())
                    .params("address",met_detail_address.getText().toString())
                    .params("mobile",met_phone.getText().toString())
                    .success(this)
                    .build()
                    .post();
        }else{
            Toast.makeText(getContext(), "数据输入有误!", Toast.LENGTH_LONG).show();
        }
    }

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        hideAllSoftInput();
        getSupportDelegate().pop();
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_new_address;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
//        met_phone.validateWith(new RegexpValidator("只能输入数字", "\\d+"));
//        met_phone.setInputType(InputType.TYPE_CLASS_PHONE);
        met_address.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (view.hasFocus()) {
                    hideAllSoftInput();
                    popCityPicker();
                } else {
                }
            }
        });
        met_address.setInputType(InputType.TYPE_NULL);
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onSuccess(String response) {
        getSupportDelegate().pop();
        addressDelegate.getSupportDelegate().pop();
        getSupportDelegate().start(new AddressDelegate());
        Toasty.success(getContext(),"成功添加收货地址!").show();
    }


    private void popCityPicker() {
        mPicker.setConfig(cityConfig);
        mPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                if (province != null) {
                }
                if (city != null) {
                }
                if (district != null) {
                    met_address.setText(province.getName() + " " + city.getName() + " " + district.getName());
                }
            }

            @Override
            public void onCancel() {
                ToastUtils.showLongToast(getContext(), "已取消");
            }
        });
        //显示
        mPicker.showCityPicker( );
    }

    private void hideAllSoftInput() {
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(getContext().INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(met_username.getWindowToken(), 0);
        imm.hideSoftInputFromWindow(met_address.getWindowToken(), 0);
        imm.hideSoftInputFromWindow(met_phone.getWindowToken(), 0);
    }

}
